package com.mysteel.order.listener;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.mysteel.order.service.OrderService;
import com.mysteel.order.vo.PayAsyncVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * ProjectName: mail
 * ClassName: OrderPayNotifyListener
 * Description:
 * date: 2021/04/06/006 13:56
 *
 * @author xiaochao
 */
@Slf4j
@RestController
public class OrderPayNotifyListener {

    @Autowired
    private OrderService orderService;

    @PostMapping("/aliPay/notify")
    public String handleAliPayNotify(PayAsyncVo asyncVo,HttpServletRequest request){
        // 支付宝异步通知回调
        log.info("支付宝异步通知回调，支付宝流水号：{}，订单号：{}",asyncVo.getTrade_no(),asyncVo.getOut_trade_no());
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 返回success后支付宝则不会再通知
        try {
            return orderService.handleAliPayNotify(asyncVo,parameterMap);
        } catch (Exception e) {
            log.error("修改订单状态失败：{}",e);
        }
        return "failed";
    }
}
